# _*_ coding: utf-8 _*_
__author__ = 'yinlei'
__date__ = '2018-05-18 8:14'
from contextlib import contextmanager

from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy
from flask_sqlalchemy import BaseQuery
from flask_wtf import CSRFProtect
from flask_mail import Mail

csrf = CSRFProtect()
mail = Mail()


class SQLALCHEMY(_SQLAlchemy):
    @contextmanager
    def auto_commit(self):
        try:
            yield
            self.session.commit()
            print('插入数据库成功')
        except Exception as e:
            self.session.rollback()
            raise e
class QueryClass(BaseQuery):
    def filter_by(self, **kwargs):
        if 'is_delete' not in kwargs.keys():
            kwargs['is_delete'] = 1
        return super(QueryClass,self).filter_by(**kwargs)

db = SQLALCHEMY(query_class=QueryClass)
